/*
 * Toedo
 * Copyright (C) 2009-2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.toedo.gui.dialogs;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.toedo.controllers.DataController;
import org.toedo.controllers.events.EventConstants;
import org.toedo.controllers.events.EventController;
import org.toedo.gui.translations.Messages;
import org.toedo.model.Attachment;
import org.toedo.model.BaseNote;

public class AttachmentsManagementDialog extends TitleAreaDialog implements PropertyChangeListener {
	
	private BaseNote mTask;
	
	private Table mAttachmentsList;

	public AttachmentsManagementDialog(Shell parentShell, BaseNote task) {
		super(parentShell);
		mTask = task;
		
		EventController.getInstance().addGeneralEventsListener(this);
	}
	
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		
		return contents;
	}
	
	protected Control createDialogArea(final Composite parent) {
		setTitle(Messages.AttachmentsManagementDialog_Title);		
		setMessage(String.format(Messages.AttachmentsManagementDialog_Message, mTask.getTitle()));
		
		Composite mainPanel = (Composite) super.createDialogArea(parent);
		mainPanel.setLayout(new GridLayout(1, false));
		
		GridData mainGD = new GridData(SWT.FILL, SWT.FILL, true, true);
		mainGD.widthHint = 500;
		mainGD.heightHint = 300;
		mainPanel.setLayoutData(mainGD);
		
		Composite panel = new Composite(mainPanel, SWT.NONE);
		panel.setLayout(new GridLayout(2, false));
		panel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		
		mAttachmentsList = new Table(panel, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
		mAttachmentsList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		
		Composite btnGroup1 = new Composite(panel, SWT.NULL);		
		btnGroup1.setLayout(new GridLayout(1, false));
		GridData gd = new GridData();
		gd.verticalAlignment = SWT.TOP;
		btnGroup1.setLayoutData(gd);
		
		Button newAttachmentBtn = new Button(btnGroup1, SWT.NONE);
		newAttachmentBtn.setText(Messages.AttachmentsManagementDialog_AddButton);
		newAttachmentBtn.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
		newAttachmentBtn.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent arg0) {
				EditAttachmentDialog dialog = new EditAttachmentDialog(parent.getShell(), mTask);
				int result = dialog.open();
				if (result == Dialog.OK) {
					DataController.getInstance().getProject().addAttachementToBaseNote(mTask, dialog.getType(), dialog.getTitle(), dialog.getLink());
				}
			}
		});
		
		Button editAttachmentBtn = new Button(btnGroup1, SWT.NONE);
		editAttachmentBtn.setText(Messages.AttachmentsManagementDialog_EditButton);
		editAttachmentBtn.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
		editAttachmentBtn.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent arg0) {
				TableItem[] selection = mAttachmentsList.getSelection();
				if ((selection != null) && 
						(selection.length > 0)) {
					
					Attachment attachment = (Attachment) selection[0].getData();
					
					if (attachment != null) {
						EditAttachmentDialog dialog = new EditAttachmentDialog(parent.getShell(), mTask, attachment);
						int result = dialog.open();
						if (result == Dialog.OK) {
							DataController.getInstance().getProject().modifyAttachmentFromBaseNote(mTask, attachment, dialog.getType(), dialog.getTitle(), dialog.getLink());
						}
					}					
				}
			}
		});
		
		Button deleteAttachmentBtn = new Button(btnGroup1, SWT.NONE);
		deleteAttachmentBtn.setText(Messages.AttachmentsManagementDialog_RemoveButton);
		deleteAttachmentBtn.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
		deleteAttachmentBtn.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent arg0) {
				TableItem[] selection = mAttachmentsList.getSelection();
				if ((selection != null) && 
						(selection.length > 0)) {
					DataController.getInstance().getProject().removeAttachmentFromBaseNote(mTask, (Attachment) selection[0].getData());
				}
			}
		});
		
		fillList();
		
		return parent;
	}
	
	private void fillList() {
		mAttachmentsList.removeAll();
		TableItem item;
		
		for (Attachment attachment : mTask.getAttachmentList()) {
			item = new TableItem(mAttachmentsList, SWT.NULL);
			item.setText(String.format("%s (%s)", attachment.getTitle(), attachment.getLink())); //$NON-NLS-1$
			item.setData(attachment);
		}
	}
	
	protected void createButtonsForButtonBar(Composite parent) {
		Button closeBtn = createButton(parent, IDialogConstants.CLOSE_ID, IDialogConstants.CLOSE_LABEL, true);
		
		closeBtn.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {				
				close();				
			}
		});
	}
	
	@Override
	public void propertyChange(PropertyChangeEvent arg0) {
		class RefreshViewer implements Runnable {
			public void run() {
				fillList();
			}
		}
		
		if ((arg0.getPropertyName().equals(EventConstants.EVT_ADD_ATTACHMENT)) ||
				(arg0.getPropertyName().equals(EventConstants.EVT_REMOVE_ATTACHMENT)) ||
				(arg0.getPropertyName().equals(EventConstants.EVT_MODIFY_ATTACHMENT))) {			
			Display.getDefault().asyncExec(new RefreshViewer());			
		}
		
	}
	
	public void dispose() {
		EventController.getInstance().removeGeneralEventsListener(this);
	}

}
